package win.demonlegion.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import win.demonlegion.service.inte.CacheService;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service("cacheService")
public class CacheServiceImpl implements CacheService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void setCache(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setCache(String key, String value, long expire) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    @Override
    public String getCache(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void deleteCache(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public void addSetValue(String key, String value) {
        redisTemplate.opsForSet().add(key, value);
    }

    @Override
    public void addSetValues(String key, String[] values) {
        redisTemplate.opsForSet().add(key, values);
    }

    @Override
    public void deleteSetValue(String key, String value) {
        redisTemplate.opsForSet().remove(key, value);
    }

    @Override
    public void deleteSetValues(String key, String[] values) {
        redisTemplate.opsForSet().remove(key, values);
    }

    @Override
    public Set<String> selectSetValues(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    @Override
    public boolean isInSet(String key, String value) {
        return redisTemplate.opsForSet().isMember(key, value);
    }

    @Override
    public long countSetSize(String key) {
        return redisTemplate.opsForSet().size(key);
    }

    @Override
    public List<String> getSubList(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    @Override
    public List<String> getList(String key) {
        return getSubList(key, 0, -1);
    }

    @Override
    public long leftPushValue(String key, String value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }

    @Override
    public long leftPushValues(String key, List<String> values) {
        return redisTemplate.opsForList().leftPushAll(key, values);
    }

    @Override
    public long rightPushValue(String key, String value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }

    @Override
    public long rightPushValues(String key, List<String> values) {
        return redisTemplate.opsForList().rightPushAll(key, values);
    }

    @Override
    public void setValue(String key, long index, String value) {
        redisTemplate.opsForList().set(key, index, value);
    }

    @Override
    public long removeAllValue(String key, String value) {
        return redisTemplate.opsForList().remove(key, 0, value);
    }

    @Override
    public long removeValueFromLeft(String key, String value) {
        // 从左到右删除第一个符合的值
        return redisTemplate.opsForList().remove(key, 1, value);
    }

    @Override
    public long removeValueFromRight(String key, String value) {
        // 从右到左删除第一个符合的值
        return redisTemplate.opsForList().remove(key, -1, value);
    }

    @Override
    public long countListSize(String key) {
        return redisTemplate.opsForList().size(key);
    }
}
